package com.jjm2473.dict;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liangbin.llb
 * @since 2020/4/1
 */
public class Tokenizer {
    private Dictionary dictionary;

    public Tokenizer(Dictionary dictionary) {
        this.dictionary = dictionary;
    }

    public List<String> tokenize(String sentence) {
        return Optional.of(sentence).filter(String::isEmpty).map(Collections::singletonList)
                .orElseGet(()-> tokenize0(sentence).collect(Collectors.toList()));
    }

    private Stream<String> tokenize0(String sentence) {
        String[] words = dictionary.lookup(sentence.charAt(0));
        return Stream.of(words).filter(sentence::startsWith)
                .flatMap(w ->
                        Optional.of(w).filter(sentence::equals).map(Stream::of)
                                .orElseGet(()->tokenize0(sentence.substring(w.length())).map(s -> w + " " + s))
                );
    }
}
